package cn.com.iresearch.android.componentizationsupporter

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import java.util.*


/**
Created By: Seal.Wu
Date: 2018/4/3
Time: 10:49
 */
class TestComponentOne : IComponent {
    override fun getComponentName(): String {
        return "TestComponentOne"
    }


    override fun execute(commandLine: String, vararg commandLineArgs: Any): Any {
        when (commandLine) {
            "toast" -> {
                val arg1 = commandLineArgs[0]
                if (arg1 is Context) {
                    Handler(Looper.getMainLooper()).post {
                        Toast.makeText(arg1, "This is ${getComponentName()}", Toast.LENGTH_LONG).show()
                    }
                    return Any()
                } else {
                    throw IllegalArgumentException("No compatible args")
                }
            }

            "log" -> {
                Log.i("ComponentizationSupporter", "This is ${getComponentName()}")
            }

            "getComponentKey" -> return "TestComponentOne"
        }

        return Any()
    }

    override fun executeAsync(commandLine: String, vararg commandLineArgs: Any, callback: (response: Any) -> Unit) {
        when (commandLine) {
            "getTime" -> {
                Thread {
                    Thread.sleep(3 * 1000)
                    callback(Date(1234567890))
                }.start()
            }
            else ->{
                callback(Unit)
            }
        }
    }

}